﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Facade.DishModule.DataTransferObject
{
    /// <summary>
    /// 每周菜肴
    /// </summary>
    public class WeekDishDTO
    {
        #region 构造函数

        public WeekDishDTO() { }

        internal WeekDishDTO(WeekDish week)
        {
            this.ID = week.ID;
            this.BeginDate = week.BeginDate;
            this.EndDate = week.EndDate;

            List<DishDTO> list = new List<DishDTO>();
            foreach (WeekDishItem item in week.WeekDishItems)
            {
                DishDTO dish = DishDTOCache.Get(item.DishID);
                if (dish != null)
                    list.Add(dish);
            }
            this.Items = list.ToArray();
        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        public DateTime BeginDate { get; set; }

        public DateTime EndDate { get; set; }

        public DishDTO[] Items { get; set; }

        #endregion

        #region 方法

        internal WeekDish ToWeekDish()
        {
            WeekDish week = new WeekDish()
            {
                BeginDate = TimeHelper.GetMinDateTime(this.BeginDate),
                EndDate = TimeHelper.GetMaxDateTime(this.EndDate),
            };

            CopyToWeekDish(week);

            return week;
        }

        internal void CopyToWeekDish(WeekDish week)
        {
            week.ClearItems();

            foreach (DishDTO dishDTO in Items)
            {
                WeekDishItem item = new WeekDishItem()
                {
                    DishID = dishDTO.ID,
                };

                week.AddItem(item);
            }
        }

        #endregion
    }
}
